﻿using System.Linq.Expressions;
using BaseRepository.Db;
using Common.DataModels;

namespace BaseService;

public class BaseService<TEntity>:IBaseService<TEntity> where TEntity:BaseEntity<TEntity>
{
    // 通过autofac属性注入的方式来获取
    protected IBaseRepository<TEntity> _baseRepository { get; set; }
    /// <summary>
    /// 查询单个对象
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    public async Task<TEntity?> GetEntityAsync(Expression<Func<TEntity, bool>> whereExpression, bool isTracking = false)
    {
        var data =  await _baseRepository.GetEntityAsync(whereExpression, isTracking);
        if (data==null)
        {
            throw new Exception("查询不到的对象");
        }

        return data;
    }
    /// <summary>
    /// 查询多个对象
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    public Task<List<TEntity>> GetEntitiesAsync(Expression<Func<TEntity, bool>> whereExpression, bool isTracking = false)
    {
        return _baseRepository.GetEntitiesAsync(whereExpression, isTracking);
    }
    /// <summary>
    /// 查询多个对象并且排序
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="orderStr"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public Task<List<TEntity>> GetEntitiesByOrderAsync(Expression<Func<TEntity, bool>> whereExpression, string orderStr = "", bool isTracking = false)
    {
        return _baseRepository.GetEntitiesByOrderAsync(whereExpression, orderStr, isTracking);
    }
    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="orderStr"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public Task<Tuple<List<TEntity>, int>> GetEntitiesByPageAsync(Expression<Func<TEntity, bool>> whereExpression, string orderStr = "", int pageIndex = 1, int pageSize = 20,
        bool isTracking = false)
    {
        return _baseRepository.GetEntitiesByPageAsync(whereExpression, orderStr, pageIndex, pageSize,isTracking);
    }
    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public Task<Tuple<List<TEntity>, int>> GetEntitiesByPageAsync(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, bool isTracking = false)
    {
        return _baseRepository.GetEntitiesByPageAsync(whereExpression, pageIndex, pageSize, isTracking);
    }
    /// <summary>
    /// 插入单条数据
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public Task<int> CreateAsync(TEntity entity)
    {
        return _baseRepository.CreateAsync(entity);
    }
    /// <summary>
    /// 插入多条数据
    /// </summary>
    /// <param name="entities"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public Task<int> CreateAsync(IEnumerable<TEntity> entities)
    {
        return _baseRepository.CreateAsync(entities);
    }
    /// <summary>
    /// 高效插入多条数据
    /// </summary>
    /// <param name="entities"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    //public Task BulkInsert(IEnumerable<TEntity> entities)
    //{
    //    return _baseRepository.BulkInsert(entities);
    //}
    /// <summary>
    /// 更新一个对象
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public Task<int> UpdateAsync(TEntity entity)
    {
        return _baseRepository.UpdateAsync(entity);
    }
    /// <summary>
    /// 更新多个对象
    /// </summary>
    /// <param name="entities"></param>
    /// <returns></returns>
    public Task<int> UpdateAsync(IEnumerable<TEntity> entities)
    {
        return _baseRepository.UpdateAsync(entities);
    }
    /// <summary>
    /// 删除单个对象
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public Task<int> DeleteAsync(long id)
    {
        return _baseRepository.DeleteAsync(id);
    }
    /// <summary>
    /// 删除多个对象
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    public Task<int> DeleteAsync(IEnumerable<long> ids)
    {
        return _baseRepository.DeleteAsync(ids);
    }
}